/**
  ******************************************************************************
  * @author     Chris
  * @since      2024/6/11 12:34
  *
  * @file       pd_timer_it.c
  * @brief      TimerIT peripheral driver.
  *
  * @note       This file contains the peripheral driver for the TimerIT.
  *
  * @warning    None.
  ******************************************************************************
  * Change Logs:
  *   Date          Author       Notes
  *   2024/6/11     Chris        the first version
  *
  ******************************************************************************
  */

#include <stm32f4xx_ll_tim.h>
#include "pd_timer_it.h"

void TimerIT_init_(const TimerIT *this, IRQn_Type irQnType, uint32_t PreemptPriority, uint32_t SubPriority) {
    // Set GPIO, RCC and Timer Base Unit.
    Timer_init(this->timer);

    LL_TIM_SetClockSource(this->timer->TIMx, LL_TIM_CLOCKSOURCE_INTERNAL);

    LL_TIM_EnableIT_UPDATE(this->timer->TIMx);

    // NVIC also can be init in main.c
    NVIC_SetPriority(irQnType, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), PreemptPriority, SubPriority));
    NVIC_EnableIRQ(irQnType);

    LL_TIM_EnableCounter(this->timer->TIMx);
}

void TimerIT_constructor(TimerIT *this, Timer *timer) {
    static struct TimerITVtbl vtbl = {
        .init = &TimerIT_init_,

    };

    this->vptr = &vtbl;

    this->timer = timer;
}
